package com.chenu.gamscriptman.controller;

import com.chenu.gamscriptman.compoent.Global;
import com.chenu.gamscriptman.domain.User;
import com.chenu.gamscriptman.domain.noindb.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author flyan
 * date 2019-12-26
 * @function 用户控制器，它用于处理用户任务完成需要视图的服务后返回的回复消息
 */
@Controller
@ApiIgnore
public class UserController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RedisTemplate jsonRedisTemplate;

    /**
     * 用户管理/列出所有用户
     */
    @GetMapping("/users")
    public String users(
            Model model
    ){
        // 放入分页数据和查询参数
        PageBean<User> user_page = (PageBean<User>) jsonRedisTemplate.opsForValue().get(Global.REDIS_USER_PAGES);
        model.addAttribute(Global.REDIS_USER_PAGES, user_page);
        Integer search_onlyRole = (Integer) jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_ONLYROLE);
        model.addAttribute(Global.REDIS_SEARCH_ONLYROLE, search_onlyRole);

        return "user/list";
    }

    /**
     * 前往添加/编辑页面
     * @param model
     * @return
     */
    @GetMapping("/user")
    public String userAddOrEdit(
            Model model
    ){
        // 放入编辑的用户
        User editUser = (User) jsonRedisTemplate.opsForValue().get(Global.REDIS_EDIT_USER);
        model.addAttribute(Global.REDIS_EDIT_USER, editUser);

        return "user/addOrEdit";
    }

}
